package com.davidniu.lesson05.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtils_C3P0 {
    private static ComboPooledDataSource datasource = null;

    static {
        try {
            /**
            //代码版配置
            datasource =   new ComboPooledDataSource();
            datasource.setDriverClass("com.mysql.jdbc.Driver");
            datasource.setUser("root");
            datasource.setPassword("123456");
            datasource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            datasource.setMaxPoolSize(10);
            datasource.setMinPoolSize(20);
            */

            datasource =   new ComboPooledDataSource("MySQL");//配置文件的写法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        return datasource.getConnection(); //从数据源中获取连接
    }
    //释放连接资源
    public static void release (Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
